package com.hc.kotlin

fun main4() {
    //-----空-----
    var a:String? = "Hello a" //可空字符串,否则,编译不通过
    a = null
    println(a)
    //-----空操作:1 ?-----
    var b:String? = "Hello b"
    println(b?.length) //若b为空,则.length不执行
    //-----:2 !!-----
    var c:String? = "Hello c"
    println(c!!.length) //人为判断不为空,强制执行
    //-----:3 ?:-----
    var d:String? = null
    var e:String? = d?:"Hello e" //为空,则使用:后的值
    println(e)
}

fun main(){
    //-----异常-----
    try {
        var a:Int? = null
        a?:throw CustomException("变量不合法") //throw 抛异常
        a!!.plus(1)
    }catch (e:Exception){ //异常捕获
        println(e)
    }
    var b:String?=null
    checkNotNull(b){"非法操作"} //内置函数
}
class CustomException(s:String):RuntimeException(s) //自定义异常